package com.zjp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjp.pojo.IVaccines;
import com.zjp.pojo.InoculationHistory;
import com.zjp.pojo.Reservation;
import com.zjp.service.ISiteAndVaccService;
import com.zjp.service.ReservationService;
import com.zjp.vo.ReservationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

/**
 * 疫苗预约接种记录模块
 */
@RestController
@Slf4j
public class ReservationController {
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private ISiteAndVaccService iSiteAndVaccService;


    /**
     * 查询预约记录
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @return
     */
    //查询预约记录
    @RequestMapping("/findAllReservationHist")
    public ModelAndView findAllReservationHist( @RequestParam(name = "pageNo",required = false,defaultValue = "1") int pageNo,
                                               @RequestParam(name = "pageSize",required = false,defaultValue = "5") int pageSize){
        ModelAndView mv=new ModelAndView();
        PageHelper.startPage(pageNo,pageSize);   //开启分页
        List<ReservationVo> reservationList=reservationService.findAllReservationHist();
        log.info("查询预约记录:{}",reservationList);

        mv.addObject("reservationList",reservationList);
        PageInfo pageInfo=new PageInfo<>(reservationList);  //存储分页信息
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("reservationHistoty");
        return mv;
    }




    /**
     * 完成接种
     *
     * @param inoculationHistory
     * @param id
     * @return
     */
    @RequestMapping("/addInoculationHistory")
    public String addInoculationHistory(InoculationHistory inoculationHistory, Reservation id){
        inoculationHistory.setInoculationTime(new Date());
        inoculationHistory.setNum(1);
        System.out.println(inoculationHistory);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //添加接种记录
        int i=reservationService.addInoculationHistory(inoculationHistory);
        if(i>0){
            //更新预约接种状态
            reservationService.updateStuts(id);
            return "SUCCESS";
        }
        return "FAIL";
    }

    /**
     * 预约记录
     */
    @RequestMapping("/cancelRese")//取消预约
    public String cancelRese(InoculationHistory inoculationHistory, int id) {
        System.out.println(inoculationHistory);
        //更新取消预约状态
        int i = reservationService.updateCancelStuts(id);
        //查询接种点疫苗表所有数据
        List<IVaccines> iVaccinesList = iSiteAndVaccService.findAllIVacc();
        log.info("查询接种点疫苗表所有数据:{}", iVaccinesList);
        for (IVaccines iVaccines : iVaccinesList) {
            //判断当前记录的疫苗编号和参数的疫苗编号是否一致 并且 判断接种点ID和参数接种点ID是否一致。
            if (iVaccines.getVaccinesid() == inoculationHistory.getVaccinesid() && iVaccines.getInoculationid() == inoculationHistory.getInoculationid()) {
                //接种点疫苗库存加一
                iSiteAndVaccService.addOne(iVaccines.getId());
            }
        }
        if (i > 0) {
            return "SUCCESS";
        }
        return "error";
    }
}
